package net.sx.minemodule.mvp.fragments;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.aroutercomment.UserService;
import com.homegoods.classify.model.Constant;
import com.homegoods.resource.HomeGoodsApp;
import com.homegoods.resource.util.Utils;
import com.kedll.library.databinding.PullableListviewBinding;
import com.kedll.library.fragment.KRefreshListFragment;
import com.kedll.library.util.MyUtils;
import com.kedll.library.util.ToastUtil;

import net.sx.minemodule.R;
import net.sx.minemodule.mvp.presenters.MineListPresenter;
import net.sx.minemodule.widget.MineHeadView;

import java.util.ArrayList;
import java.util.List;


/**
 * 我的 模块
 *
 * @author 11470
 */
@Route(path = ARouterPath.PATH_FRAGMENT_MY)
public class MineFragment extends KRefreshListFragment<PullableListviewBinding, MineListPresenter> {
    MineHeadView mineHead;
    @Autowired(name = ARouterPath.PATH_USER_SERVICE)
    public UserService userService;

    @Override
    protected void initEvent(Bundle savedInstanceState) {
        super.initEvent(savedInstanceState);
        ARouter.getInstance().inject(this);
        //设置
        mineHead.setSettingClickListener(v ->
                ARouter.getInstance().build(ARouterPath.PATH_MINE_SETTING).navigation());

        //del shibo.zheng 19/06/13
        //超级会员
//        mineHead.setSuperMemberLayoutClickListener(v ->
//                ARouter.getInstance().build(ARouterPath.PATH_MINE_SUPER_MEMBER).navigation());

        //全部订单
        mineHead.setAllOrderClickListener(v -> showMineOrder(0));

        //待付款
        mineHead.setWaitPayOrderClickListener(v -> showMineOrder(1));

        //待发货
        mineHead.setWaitDeliverOrderClickListener(v -> showMineOrder(2));

        //待收货
        mineHead.setWaitReceivingOrderClickListener(v -> showMineOrder(3));

        //待评价
        mineHead.setEvaluateOrderClickListener(v -> showMineOrder(4));

        //退款/售后
        mineHead.setAfterSaleOrderClickListener(v ->
                ARouter.getInstance().build(ARouterPath.PATH_MINE_AFTER_LIST).navigation());

        //我的收藏
        mineHead.setGoodsCollectClickListener(v ->
                ARouter.getInstance().build(ARouterPath.PATH_MINE_COLLECT).navigation());

        //del shibo.zheng start 19/06/13
        /*//我的大洋
        mineHead.setMoneyMineClickListener(v ->
                ARouter.getInstance().build(ARouterPath.PATH_MINE_INTEGRAL).navigation());

        //我的收益
        mineHead.setPayGiftMineClickListener(v -> ARouter.getInstance().build(ARouterPath.PATH_MINE_YIELD).navigation());

        //我的账户
        mineHead.setPackageMineClickListener(v ->
                ARouter.getInstance().build(ARouterPath.PATH_MINE_PACKAGE).navigation());

        //客服与反馈
        mineHead.setMsgMineClickListener(v -> {
            ARouter.getInstance().build(ARouterPath.PATH_MINE_SERVERANDFEEDBACK).navigation();
        });

        //关于我们
        mineHead.setAboutMineClickListener(v -> ARouter.getInstance().build(ARouterPath.PATH_MINE_ABOUT_MINE).navigation());


        //我的二维码
        mineHead.setGiftMineClickListener(v -> ARouter.getInstance().build(ARouterPath.PATH_MINE_TWO_CODE).navigation());*/
        //del shibo.zheng end 19/06/13

        //add shibo.zheng start 19/06/13
        //我的积分
        mineHead.setIntegralMineClickListener(v -> ARouter.getInstance().build(ARouterPath.PATH_MINE_INTEGRAL).navigation());

        //我的佣金
        mineHead.setBrokerageMineMineClickListener(v -> ARouter.getInstance().build(ARouterPath.PATH_MINE_BROKERAGE).navigation());

        //我的奖牌
        mineHead.setMedalMineMineClickListener(v -> ARouter.getInstance().build(ARouterPath.PATH_MINE_MEDAL).navigation());

        //我的推广码
        mineHead.setPromotionCodeMineMineClickListener(v -> ARouter.getInstance().build(ARouterPath.PATH_MINE_PROMOTION_CODE).navigation());

        //我是调研员
//        mineHead.setResearcherMineMineMineClickListener(v -> ARouter.getInstance().build(ARouterPath.PATH_MINE_RESEARCHER).navigation());
        mineHead.setResearcherMineMineMineClickListener(v -> ARouter.getInstance().build(ARouterPath.PATH_BECOME_RESEARCHER).navigation());
        //add shibo.zheng end 19/06/13

        //个人主页
        mineHead.setStyListHomeClickListener(v ->
                ARouter.getInstance().build(ARouterPath.PATH_DESIGNER_HOME).navigation());

        //我的作品
        mineHead.setProductionMineClickListener(v ->
                ARouter.getInstance().build(ARouterPath.PATH_MY_DESIGN).navigation());

        //我的粉丝
        mineHead.setAttentionListClickListener(v ->
                ARouter.getInstance().build(ARouterPath.PATH_MINE_MYFANS_LIST).navigation());

        list.addItemDecoration(new SpacesItemDecoration(2, 8));

        mineHead.setUserHead(userService.getHeadImg());
        mineHead.setName(userService.getNiName());
        mineHead.setMemberType(userService.getRoleName());
        onRefresh();
    }

    private void showMineOrder(int i) {
        ARouter.getInstance().build(ARouterPath.PATH_MY_ORDER)
                .withInt("orderType", i)
                .navigation();
    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return Utils.getGridLayoutManager(getContext(), adapter, 2);
    }

    @Override
    protected List<View> getListHeadView() {
        List<View> views = new ArrayList<>();
        mineHead = new MineHeadView(getContext());
        views.add(mineHead);
        return views;
    }

    @Override
    public void onUserInvisible() {
        super.onUserInvisible();
        if (getActivity() != null) {
            MyUtils.setSystemUi(getActivity().getWindow(), true);
        }
    }

    @Override
    public void onUserVisible() {
        super.onUserVisible();
        if (getUserVisibleHint()) {
            if (getUserVisibleHint()){
                if (!HomeGoodsApp.isLogin()) {
                    ToastUtil.showToast("uuuuuuuuuuuu");
                }
            }
        }
        if (getActivity() != null) {
            MyUtils.setSystemUi(getActivity().getWindow(), false);
        }
    }

    @Override
    protected MineListPresenter getPresenter() {
        //在这里加载数据
        return new MineListPresenter(this);
    }

    @Override
    protected int getLayout() {
//        return R.layout.mine_fragment_mine;
        return R.layout.pullable_listview;
    }

    @Override
    public void onFirstUserVisible() {
        super.onFirstUserVisible();
        if (getUserVisibleHint()){
            if (!HomeGoodsApp.isLogin()) {
                ToastUtil.showToast("ffffffffff");
            }
        }
        if (getActivity() != null) {
            MyUtils.setSystemUi(getActivity().getWindow(), false);
        }
    }

    private class SpacesItemDecoration extends RecyclerView.ItemDecoration {

        private int space;
        private int itemCount;

        public SpacesItemDecoration(int itemCount, int space) {
            this.itemCount = itemCount;
            this.space = MyUtils.dp2px(space);

        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            if (parent.getChildLayoutPosition(view) == 0) {
                outRect.set(0, 0, 0, 0);
            } else {
                outRect.bottom = space;
                //当前是推荐类商品，获取位置时，要除去购物车里的商品数量
                int position = parent.getChildLayoutPosition(view) - list.getHeaderItemCount();
                if (position % itemCount == 0) {
                    outRect.left = 0;
                    outRect.right = space / 2;
                } else if (position % itemCount == itemCount - 1) {
                    outRect.right = 0;
                    outRect.left = space / 2;
                } else {
                    outRect.right = space / 2;
                    outRect.left = space / 2;
                }
            }
        }
    }
}
